In Class Exercise 06

Fundamentals of Visual Analytics

Shachi Anirudha Raodeo https://github.com/ShachiR/ISSS608 (Visual Analytics and Applications)
05-25-2022

Getting Started

Setting up R packages

Packages ggstatsplot and [tidyverse] (https://www.tidyverse.org/) launched using library()

packages = c('ggstatsplot', 'tidyverse','tmap','sf','lubridate','clock','sftime','rmarkdown')
for (p in packages){
  if(!require(p, character.only = T)){
    install.packages(p)
  }
  library(p,character.only = T)
}

Importing Data

schools <- read_sf("data/Schools.csv", 
                   options = "GEOM_POSSIBLE_NAMES=location")
pubs <- read_sf("data/Pubs.csv", 
                   options = "GEOM_POSSIBLE_NAMES=location")
restaurants <- read_sf("data/Restaurants.csv", 
                   options = "GEOM_POSSIBLE_NAMES=location")
buildings <- read_sf("data/Buildings.csv", 
                   options = "GEOM_POSSIBLE_NAMES=location")
apartments <- read_sf("data/Apartments.csv", 
                   options = "GEOM_POSSIBLE_NAMES=location")
employers <- read_sf("data/Employers.csv", 
                   options = "GEOM_POSSIBLE_NAMES=location")

Working with sf

Anova Test

plot(schools)

plot(buildings)

plot(employers)

plot(pubs)

tmap_mode("view")
tm_shape(buildings)+
tm_polygons(col = "grey60",
           size = 1,
           border.col = "black",
           border.lwd = 1)
tmap_mode("plot")
tmap_mode("view")
tm_shape(buildings)+
tm_polygons(col = "grey60",
           size = 1,
           border.col = "black",
           border.lwd = 1) +
tm_shape(employers) +
  tm_dots(col = "red")
logs_selected <- read_rds("data/logs_selected.rds")
hex <- st_make_grid(buildings, 
                    cellsize=100, 
                    square=FALSE) %>%
  st_sf() %>%
  rowid_to_column('hex_id')
plot(hex)

points_in_hex <- st_join(logs_selected, 
                         hex, 
                         join=st_within) %>%
  st_set_geometry(NULL) %>%
  count(name='pointCount', hex_id)
head(points_in_hex)
# A tibble: 6 × 2
  hex_id pointCount
   <int>      <int>
1    169         35
2    212         56
3    225         21
4    226         94
5    227         22
6    228         45
hex_combined <- hex %>%
  left_join(points_in_hex, 
            by = 'hex_id') %>%
  replace(is.na(.), 0)
tm_shape(hex_combined %>%
           filter(pointCount > 0))+
  tm_fill("pointCount",
          n = 8,
          style = "quantile") +
  tm_borders(alpha = 0.1)
logs_path <- logs_selected %>%
  group_by(participantId, day) %>%
  summarize(m = mean(Timestamp), 
            do_union=FALSE) %>%
  st_cast("LINESTRING")
print(logs_path)
Simple feature collection with 5781 features and 3 fields
Geometry type: LINESTRING
Dimension:     XY
Bounding box:  xmin: -4616.828 ymin: 35.4377 xmax: 2630 ymax: 7836.546
CRS:           NA
# A tibble: 5,781 × 4
# Groups:   participantId [1,011]
   participantId   day m                               currentLocation
   <chr>         <int> <dttm>                             <LINESTRING>
 1 0                 1 2022-03-01 13:34:23 (-2721.353 6862.861, -2689…
 2 0                 2 2022-03-02 14:19:50 (-2721.353 6862.861, -2689…
 3 0                 3 2022-03-03 13:39:13 (-2721.353 6862.861, -2689…
 4 0                 4 2022-03-04 13:38:11 (-2721.353 6862.861, -2689…
 5 0                 5 2022-03-05 13:08:02 (-2721.353 6862.861, -2689…
 6 0                 6 2022-03-06 06:28:00 (-2721.353 6862.861, -2689…
 7 1                 1 2022-03-01 18:07:24 (-1531.133 5597.244, -1863…
 8 1                 2 2022-03-02 16:57:05 (-2619.036 5860.49, -2200.…
 9 1                 3 2022-03-03 14:13:40 (-260.4575 5026.151, -352.…
10 1                 4 2022-03-04 14:31:45 (-3903.194 5967.837, -3655…
# … with 5,771 more rows
logs_path_selected <- logs_path %>%
  filter(participantId==0)
tmap_mode("plot")
tm_shape(buildings)+
tm_polygons(col = "grey60",
           size = 1,
           border.col = "black",
           border.lwd = 1)+
tm_shape(logs_path_selected)+
tm_lines(col = "blue")+
tmap_mode("plot")